//
//  PersonalSettingVC.m
//  Cunli
//
//  Created by lilubin on 15/6/29.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "PersonalSettingVC.h"
#import "ChangeLoginPwdVC.h"
#import "MyDataService.h"
#import "Config.h"
#import "NSString+MD5.h"
#import "MBProgressHUD.h"
#import "helpListPageVC.h"
#import "ChangePersonalInfoVC.h"
#import "Config.h"
#import "ChangeMobieVC.h"
#import "UMessage.h"
#import "AboutAppVC.h"
#import "FeedbackVC.h"
#import "UIColor+expanded.h"
#import "LoginVC_new.h"

//#define KCLLOGINCHANGE @"myloginchange"
//#define KCLLOGINOUT @"loginout"

@interface PersonalSettingVC ()
{
    NSIndexPath *_oldSelIndexPath;
}

@end

@implementation PersonalSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavAttributes];
    
    self.title = @"设置";
    self.clearsSelectionOnViewWillAppear = NO;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"0xf6f6f6"];
    //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.sectionHeaderHeight = 2;
    self.tableView.sectionFooterHeight = 2;
}

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setNavAttributes
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    else if(section == 1)
    {
        return 4;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    NSArray *titles = @[
                        @[@"个人资料修改", @"已绑定手机:", @"登录密码修改"],
                        @[@"使用帮助", @"反馈", @"评价我们",  @"关于村里"],
                        @[@"退出登录"],
                        ];
    
    //    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = titles[indexPath.section][indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    
    /*
     if (indexPath.section == 2) {
     cell.textLabel.textAlignment = NSTextAlignmentCenter;
     cell.backgroundColor = [UIColor orangeColor];
     [cell.textLabel setTextColor:[UIColor whiteColor]];
     [cell.textLabel setFont:[UIFont boldSystemFontOfSize:19]];
     cell.accessoryType = UITableViewCellAccessoryNone;
     }
     */
    
    if (indexPath.section == 0 && indexPath.row == 1) {
        NSRange reRange = NSMakeRange(2, 5);
        NSString *textWithNum = [[Config getOwnUsername] stringByReplacingCharactersInRange:reRange withString:@"*****"];
        cell.textLabel.text = [cell.textLabel.text stringByAppendingString:textWithNum];
    }
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                ChangePersonalInfoVC *personalInfoVC = [[ChangePersonalInfoVC alloc] init];
                [self.navigationController pushViewController:personalInfoVC animated:YES];
                break;
            }
            case 1:
            {
                ChangeMobieVC *changeMobileVC = [[ChangeMobieVC alloc] init];
                [self.navigationController pushViewController:changeMobileVC animated:YES];
                break;
            }
            case 2:
            {
                ChangeLoginPwdVC *changLoginPwdVC = [[ChangeLoginPwdVC alloc] init];
                [self.navigationController pushViewController:changLoginPwdVC animated:YES];
                break;
            }
            default:
                break;
        }
    }
    else if(indexPath.section == 1)
    {
        switch (indexPath.row) {
            case 0:
            {
                helpListPageVC *helpListVC = [[helpListPageVC alloc] init];
                [self.navigationController pushViewController:helpListVC animated:YES];
                break;
            }
            case 1:
            {
                FeedbackVC *feedBackViewController = [[FeedbackVC alloc] init];
                [self.navigationController pushViewController:feedBackViewController animated:YES];
                break;
            }
            case 2:
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/cun-li-yi-dong-hu-lian-wang/id1024868113?mt=8"]];
                break;
            }
            case 3:
            {
                AboutAppVC *aboutApp = [[AboutAppVC alloc] init];
                [self.navigationController pushViewController:aboutApp animated:YES];
            }
            default:
                break;
        }
    }
    else //退出方法
    {
        [Config removeAutoLogin];
        [Config saveAutoLogin:NO];
        [Config removeLogin];
        [self loginout];
    }
    
    _oldSelIndexPath = indexPath;
}

- (void)loginout
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"tongXunLuArray"];
    NSArray *areaidsArray = [[Config getOwnAreaids] componentsSeparatedByString:@","];
    [UMessage removeTag:areaidsArray response:^(id responseObject, NSInteger remain, NSError *error) {
        //NSLog(@"添加APSN失败:%@", error.userInfo);
    }];
    //      [UMessage unregisterForRemoteNotifications];
    EMError *error = nil;
    NSDictionary *info = [[EaseMob sharedInstance].chatManager logoffWithUnbindDeviceToken:YES error:&error];
    if (!error && info) {
        NSLog(@">>>>>>>退出成功");
    }
    
    LoginVC_new *loginVC = [[LoginVC_new alloc] init];
    loginVC.netCheck = @"";
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.rootViewController = loginVC;
    [keyWindow makeKeyAndVisible];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_oldSelIndexPath != nil && _oldSelIndexPath.section == 0 && _oldSelIndexPath.row == 1) {
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:_oldSelIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

@end
